package com.example.mobilesafe.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.example.mobilesafe.R;
import com.example.mobilesafe.constant.ConstantValue;
import com.example.mobilesafe.service.LockScreenService;
import com.example.mobilesafe.utils.ServiceUtil;
import com.example.mobilesafe.utils.SharedPreferencesUtil;

public class ProcessSettingActivity extends AppCompatActivity {

    private CheckBox cb_show_system;

    private CheckBox cb_lock_clean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_setting);

        // 初始化“隐藏系统进程”
        initSystemShow();

        // 初始化“锁屏时清理系统进程”
        initLockScreenClear();
    }

    /**
     * 隐藏系统进程
     */
    private void initSystemShow() {
        cb_show_system = findViewById(R.id.cb_show_system);
        // 对之前存储过的状态进行回显
        boolean showSystem = SharedPreferencesUtil.getBoolean(this, ConstantValue.SHOW_SYSTEM, false);
        cb_show_system.setChecked(showSystem);
        if (showSystem){
            cb_show_system.setText("显示系统进程");
        }else {
            cb_show_system.setText("隐藏系统进程");
        }
        // 对选中状态进行监听
        cb_show_system.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // isChecked作为是否选中的状态
                if (isChecked){
                    cb_show_system.setText("显示系统进程");
                }else {
                    cb_show_system.setText("隐藏系统进程");
                }
                SharedPreferencesUtil.putBoolean(getApplicationContext(), ConstantValue.SHOW_SYSTEM,isChecked);
            }
        });
    }

    /**
     * 锁屏时清理进程
     */
    private void initLockScreenClear() {
        cb_lock_clean = findViewById(R.id.cb_lock_clean);
        // 根据锁屏清理的服务是否开启，决定单选框是否选中
        boolean isRunning = ServiceUtil.isRunning(this, "com.example.mobilesafe.service.LockScreenService");
        if (isRunning){
            cb_lock_clean.setText("锁屏清理已开启");
        }else {
            cb_lock_clean.setText("锁屏清理已关闭");
        }
        // 维护CheckBox的选中状态
        cb_lock_clean.setChecked(isRunning);
        // 对选中状态进行监听
        cb_lock_clean.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // isChecked作为是否选中的状态
                if (isChecked){
                    cb_lock_clean.setText("锁屏清理已开启");
                    // 开启服务
                    startService(new Intent(getApplicationContext(),LockScreenService.class));
                }else {
                    cb_lock_clean.setText("锁屏清理已关闭");
                    // 关闭服务
                    stopService(new Intent(getApplicationContext(), LockScreenService.class));
                }
            }
        });
    }
}